import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		isAlreadyLogin: false,
		openId: "",
		sessionId: undefined,
		autor: {
			clientId: "",
		},
		requestOption: undefined,
		distributor: undefined,
	},
	mutations: {
		setDistributor(state, distributor) {
			state.distributor = distributor;
		},
		clearDistributor(state) {
			state.distributor = undefined;
		},
		setRequestOption(state, requestOption) {
			state.requestOption = requestOption;
			state.sessionId = undefined;
		},
		removeRequestOption(state) {
			state.requestOption = undefined;
		},
		removeSessionId(state) {
			state.sessionId = "";
		},
		setSessionId(state, sessionId) {
			state.sessionId = sessionId;
		},
		saveOpenId(state, openId) {
			state.openId = openId;
		},
		loginSuccess(state, autor) {
			state.isAlreadyLogin = true;
			state.autor = autor;
			
			if (state.requestOption) {
				state.requestOption.header["Cookie"] = "JSESSIONID="+state.sessionId;
				uni.request(state.requestOption);
				state.requestOption = undefined;
			}
		},
		loginOut(state) {
			state.isAlreadyLogin = false;
			state.autor = undefined;
		}
	}
});
export default store;